package cn.luo.service;

import cn.luo.dao.UserDao;
import cn.luo.entity.Blogs;
import cn.luo.dao.FileDao;
import cn.luo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    UserDao userDao;

    @Autowired
    FileDao fileDao;

    @Autowired
    PasswordEncoder pe;

    //添加用户
    @Override
    public int addUser(User user, MultipartFile multipartFile) {
        String str = fileDao.SaveMultipartFile(multipartFile);
        User usr = userDao.findUserByName(user.getUsername());
        System.out.println("str = " + str);
        if (user.getPassword()!=null){
            user.setPassword(pe.encode(user.getPassword()));
        }
        user.setImg(str);
        //用户名不存在就不注册
        if(usr!=null){
            return 0;
        }
        userDao.addUser(user);
        //返回1 状态，代表注册成功
        return 1;
    }

    //根据用户名查询用户信息
    @Override
    public User findUserByName(String username) {
        return userDao.findUserByName(username);
    }

    //分页查询
    @Override
    public List<Blogs> finfByPage2(int start, int num) {
        Integer startx = (start-1) * num;
        return userDao.findByPage2(startx,num);
    }

    @Override
    public long total() {
        return userDao.total();
    }
}
